﻿using Landpy.CruiseControlNet.ConfigurationSidekick.ParserModule;
using Landpy.CruiseControlNet.ConfigurationSidekick.Resources;
using Landpy.CruiseControlNet.FacadeInterface.ConfigurationModule;

namespace Landpy.CruiseControlNet.ConfigurationSidekick.ConfigTreeModule
{
    class EmailHandler : UpdateChildrenNode
    {
        public override void UpdateChildrenNodes(ChildrenNodeContext childrenNodeContext)
        {
            var treeNodeIndentity = this.GetTreeNodeIndentity(childrenNodeContext);
            var treeNodesBuilder = this.GetTreeNodesBuilder(childrenNodeContext.CruiseControl);
            if (treeNodeIndentity.Type == TreeNodeType.EmailPublisher)
            {
                if (childrenNodeContext.ChangedValueKey == typeof(Group).Name)
                {
                    var emailGroupsTreeNode = childrenNodeContext.SelectedNode.Nodes[Resource.TreeNode_Groups];
                    emailGroupsTreeNode.Nodes.Clear();
                    ProjectChildCollectionKey publisherKey = ProjectChildCollectionKeyUtil.GetKey(treeNodeIndentity.Key);
                    var queryProject = this.ProjectRepository.GetProjectById(childrenNodeContext.CruiseControl, publisherKey.ProjectId);
                    if (queryProject != null)
                    {
                        var email = this.PublisherRepository.GetPublisherById(queryProject, publisherKey.Id) as Email;
                        if (email != null && email.Groups != null)
                        {
                            foreach (var group in email.Groups)
                            {
                                treeNodesBuilder.BuildGroupNode(group, emailGroupsTreeNode, publisherKey.ProjectId, publisherKey.Id);
                            }
                        }
                    }
                    childrenNodeContext.SelectedNode.Expand();
                    emailGroupsTreeNode.Expand();
                }
                else if (childrenNodeContext.ChangedValueKey == typeof(User).Name)
                {
                    var emailUsersTreeNode = childrenNodeContext.SelectedNode.Nodes[Resource.TreeNode_Users];
                    emailUsersTreeNode.Nodes.Clear();
                    ProjectChildCollectionKey publisherKey = ProjectChildCollectionKeyUtil.GetKey(treeNodeIndentity.Key);
                    var queryProject = this.ProjectRepository.GetProjectById(childrenNodeContext.CruiseControl, publisherKey.ProjectId);
                    if (queryProject != null)
                    {
                        var email = this.PublisherRepository.GetPublisherById(queryProject, publisherKey.Id) as Email;
                        if (email != null && email.Users != null)
                        {

                            foreach (var group in email.Users)
                            {
                                treeNodesBuilder.BuildUserNode(group, emailUsersTreeNode, publisherKey.ProjectId, publisherKey.Id);
                            }
                        }
                    }
                    childrenNodeContext.SelectedNode.Expand();
                    emailUsersTreeNode.Expand();
                }
                else if (childrenNodeContext.ChangedValueKey == typeof(NotificationType).Name)
                {
                    var emailModifierNotificationTypesTreeNode = childrenNodeContext.SelectedNode.Nodes[Resource.TreeNode_ModifierNotificationTypes];
                    emailModifierNotificationTypesTreeNode.Nodes.Clear();
                    ProjectChildCollectionKey publisherKey = ProjectChildCollectionKeyUtil.GetKey(treeNodeIndentity.Key);
                    var queryProject = this.ProjectRepository.GetProjectById(childrenNodeContext.CruiseControl, publisherKey.ProjectId);
                    if (queryProject != null)
                    {
                        var email = this.PublisherRepository.GetPublisherById(queryProject, publisherKey.Id) as Email;
                        if (email != null && email.Users != null)
                        {

                            foreach (var notificationType in email.ModifierNotificationTypes)
                            {
                                treeNodesBuilder.BuildNotificationTypeNode(notificationType, emailModifierNotificationTypesTreeNode);
                            }
                        }
                    }
                    childrenNodeContext.SelectedNode.Expand();
                    emailModifierNotificationTypesTreeNode.Expand();
                }
                else if (childrenNodeContext.ChangedValueKey == typeof(Subject).Name)
                {
                    var emailSubjectSettingsTreeNode = childrenNodeContext.SelectedNode.Nodes[Resource.TreeNode_SubjectSettings];
                    emailSubjectSettingsTreeNode.Nodes.Clear();
                    ProjectChildCollectionKey publisherKey = ProjectChildCollectionKeyUtil.GetKey(treeNodeIndentity.Key);
                    var queryProject = this.ProjectRepository.GetProjectById(childrenNodeContext.CruiseControl, publisherKey.ProjectId);
                    if (queryProject != null)
                    {
                        var email = this.PublisherRepository.GetPublisherById(queryProject, publisherKey.Id) as Email;
                        if (email != null && email.Users != null)
                        {
                            int subjectIndex = 0;
                            foreach (var subject in email.SubjectSettings)
                            {
                                treeNodesBuilder.BuildSubjectNode(subject, emailSubjectSettingsTreeNode, publisherKey.ProjectId, publisherKey.Id);
                                subjectIndex++;
                            }
                        }
                    }
                    childrenNodeContext.SelectedNode.Expand();
                    emailSubjectSettingsTreeNode.Expand();
                }
                else if (childrenNodeContext.ChangedValueKey == typeof(XslFile).Name)
                {
                    var emailXslFilesTreeNode = childrenNodeContext.SelectedNode.Nodes[Resource.TreeNode_XslFiles];
                    emailXslFilesTreeNode.Nodes.Clear();
                    ProjectChildCollectionKey publisherKey = ProjectChildCollectionKeyUtil.GetKey(treeNodeIndentity.Key);
                    var queryProject = this.ProjectRepository.GetProjectById(childrenNodeContext.CruiseControl, publisherKey.ProjectId);
                    if (queryProject != null)
                    {
                        var email = this.PublisherRepository.GetPublisherById(queryProject, publisherKey.Id) as Email;
                        if (email != null && email.Users != null)
                        {
                            foreach (var file in email.XslFiles)
                            {
                                treeNodesBuilder.BuildFileNode(file, emailXslFilesTreeNode, publisherKey.ProjectId, publisherKey.Id);
                            }
                        }
                    }
                    childrenNodeContext.SelectedNode.Expand();
                    emailXslFilesTreeNode.Expand();
                }
            }
            else
            {
                this.Successor.UpdateChildrenNodes(childrenNodeContext);
            }
        }
    }
}
